home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 6 / develop 6 code / TCP / NewsWatcher / NewsWatcher 2.0d15 source / source / subscribe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-30  |  5.5 KB  |  205 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     subscribe.c
  4.  
  5.     This module handles subscribing and unsubscribing to groups.
  6.     
  7.     Portions copyright © 1990, Apple Computer.
  8.     Portions copyright © 1993, Northwestern University.
  9.  
  10. ----------------------------------------------------------------------------*/
  11.  
  12. #include <string.h>
  13.  
  14. #include "glob.h"
  15. #include "dlgutil.h"
  16. #include "child.h"
  17. #include "close.h"
  18. #include "resize.h"
  19. #include "subscribe.h"
  20. #include "util.h"
  21. #include "nntp.h"
  22. #include "wind.h"
  23.  
  24.  
  25.  
  26. /*----------------------------------------------------------------------------
  27.     AddNewGroup 
  28.     
  29.     Adds a new group to a user group window.
  30.     
  31.     Entry:    nameOffset = offset in gGroupNames of group name to add.
  32.             wind = pointer to user group window.
  33.             pos = position of new user group list entry (row number
  34.                 of new cell). Pass 0x7fff to add at end of list.
  35.  
  36.     Exit:    function result = 
  37.                 0 if new group added.
  38.                 1 if group already present in window.
  39.                 2 if some other error.
  40. ----------------------------------------------------------------------------*/
  41.  
  42. static short AddNewGroup (long nameOffset, WindowPtr wind, short pos)
  43. {
  44.     ListHandle theList;
  45.     Cell theCell;
  46.     TWindow **info;
  47.     TGroup **groupArray, newGroup;
  48.     short numCells, cellData, cellDataLen, newGroupIndex;
  49.     Handle strings;
  50.     TUnread **unread;
  51.     CStr255 groupName;
  52.  
  53.     info = (TWindow**)GetWRefCon(wind);
  54.     theList = (**info).theList;
  55.     numCells = (**theList).dataBounds.bottom;
  56.     groupArray = (**info).groupArray;
  57.     strings = (**info).strings;
  58.     strcpy(groupName, *strings + nameOffset);
  59.  
  60.     theCell.h = 0;
  61.     for (theCell.v = 0; theCell.v < numCells; theCell.v++) {
  62.         cellDataLen = 2;
  63.         LGetCell(&cellData, &cellDataLen, theCell, theList);
  64.         if (strcmp(groupName, *strings + (*groupArray)[cellData].nameOffset) == 0) return 1;
  65.     }
  66.     
  67.     newGroup.nameOffset = nameOffset;
  68.     newGroup.firstMess = newGroup.lastMess = newGroup.numUnread = 0;
  69.     newGroup.onlyRedrawCount = false;
  70.     if (GetGroupArticleRange(&newGroup) == 2) return 2;
  71.     if (newGroup.firstMess <= newGroup.lastMess) {
  72.         unread = (TUnread**)MyNewHandle(sizeof(TUnread));
  73.         (**unread).firstUnread = newGroup.firstMess;
  74.         (**unread).lastUnread = newGroup.lastMess;
  75.         (**unread).next = nil;
  76.         newGroup.unread = unread;
  77.     } else {
  78.         newGroup.unread = nil;
  79.     }
  80.  
  81.     MySetHandleSize((Handle)groupArray, GetHandleSize((Handle)groupArray) + sizeof(TGroup));
  82.     newGroupIndex = (**info).numGroups;
  83.     (**info).numGroups++;
  84.     (*groupArray)[newGroupIndex] = newGroup;
  85.  
  86.     LDoDraw(false, theList);
  87.     pos = LAddRow(1, pos, theList);
  88.     SetPt(&theCell, 0, pos);
  89.     LSetCell(&newGroupIndex, 2, theCell, theList);
  90.     LDoDraw(true, theList);
  91.  
  92.     SetPort(wind);
  93.     InvalRect(&wind->portRect);
  94.     
  95.     (**info).changed = true;
  96.     return 0;
  97. }
  98.  
  99.  
  100.  
  101. /*----------------------------------------------------------------------------
  102.     SubscribeSelected 
  103.     
  104.     Subscribes to all selected newsgroups.
  105.     
  106.     Entry:    srcWindow = pointer to source window.
  107.             destWindow = pointer to destination window.
  108.             pos = position of new user group list entries in destination
  109.                 window (starting row number of new cells). 
  110.                 Pass 0x7fff to add at end of list.
  111. ----------------------------------------------------------------------------*/
  112.  
  113. void SubscribeSelected (WindowPtr srcWindow, WindowPtr destWindow, short pos)
  114. {
  115.     Cell srcCell;
  116.     TWindow **srcInfo;
  117.     TGroup **srcGroupArray;
  118.     ListHandle srcList;
  119.     short cellData, cellDataLen;
  120.     short result;
  121.     short numSelected=0, numSubscribed=0;
  122.     
  123.     srcInfo = (TWindow**)GetWRefCon(srcWindow);
  124.     srcList = (**srcInfo).theList;
  125.     srcGroupArray = (**srcInfo).groupArray;
  126.     SetPt(&srcCell,0,0);
  127.     while (LGetSelect(true, &srcCell, srcList)) {
  128.         numSelected++;
  129.         cellDataLen = 2;
  130.         LGetCell(&cellData, &cellDataLen, srcCell, srcList);
  131.         result = AddNewGroup((*srcGroupArray)[cellData].nameOffset , destWindow, pos);
  132.         if (result == 2) return;
  133.         if (result == 0) numSubscribed++;
  134.         srcCell.v++;
  135.         pos++;
  136.     }
  137.     if (numSubscribed > 0) SetWindowNeedsZooming(destWindow);
  138.     if (numSelected == numSubscribed) return;
  139.     if (numSelected == 1) {
  140.         ErrorMessage("That group has already been subscribed to.");
  141.     } else if (numSubscribed > 0) {
  142.         ErrorMessage("One or more of those groups have already been subscribed to.");
  143.     } else {
  144.         ErrorMessage("All of those groups have already been subscribed to.");
  145.     }
  146.     return;
  147. }
  148.  
  149.  
  150.  
  151. /*----------------------------------------------------------------------------
  152.     DoSubscribe 
  153.     
  154.     Handles the Subscribe command.
  155.     
  156.     Entry:    wind = pointer to group list window.
  157. ----------------------------------------------------------------------------*/
  158.  
  159. void DoSubscribe (WindowPtr wind)
  160. {
  161.     TWindow **info, **destInfo;
  162.     WindowPtr destWindow;
  163.     
  164.     info = (TWindow**)GetWRefCon(wind);
  165.         
  166.     destWindow = (WindowPtr) ((WindowPeek)wind)->nextWindow;
  167.     while (destWindow != nil) {
  168.         destInfo = (TWindow**)GetWRefCon(destWindow);
  169.         if ((**destInfo).kind == kUserGroup) break;
  170.         destWindow = (WindowPtr)((WindowPeek)destWindow)->nextWindow;
  171.     }
  172.     if (!destWindow) return;
  173.     
  174.     SubscribeSelected(wind, destWindow, 0x7fff);
  175. }
  176.  
  177.  
  178.  
  179. /*----------------------------------------------------------------------------
  180.     DoUnsubscribe 
  181.     
  182.     Handles the Unsubscribe command.
  183.     
  184.     Entry:    wind = pointer to user group list window.
  185. ----------------------------------------------------------------------------*/
  186.  
  187. void DoUnsubscribe (WindowPtr wind)
  188. {
  189.     WindowPtr child;
  190.     TWindow **info;
  191.     ListHandle theList;
  192.     Cell theCell;
  193.     
  194.     info = (TWindow**)GetWRefCon(wind);
  195.     theList = (**info).theList;
  196.     
  197.     SetPt(&theCell,0,0);
  198.     while (LGetSelect(true, &theCell, theList)) {
  199.         if ((child = FindChild(wind, theCell)) != nil) DoCloseWindow(child);
  200.         LDelRow(1, theCell.v, theList);
  201.         (**info).changed = true;
  202.     }
  203. }
  204.  
  205.